package com.jrx.anytxn.reporting.job.xib.step;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.entity.AmRepaymentPlanInfo;
import com.jrx.anytxn.reporting.entity.CmInteCoLonRepayPlanCi;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @comment: 贷款还款计划Processor
 * @author: hsoong
 * @date: 2021/7/2 10:16
 */
@Service
public class RepayPlanCiProcessor implements ItemProcessor<AmRepaymentPlanInfo, CmInteCoLonRepayPlanCi> {


    @Autowired
    private IOrganizationTableService organizationTableService;

    private static final Logger logger = LoggerFactory.getLogger(RepayPlanCiProcessor.class);


    @Override
    public CmInteCoLonRepayPlanCi process(AmRepaymentPlanInfo planInfo) throws Exception {
        try {
            if (null != planInfo || StringUtils.isNotBlank(planInfo.getLoanId())) {
                PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
                CmInteCoLonRepayPlanCi plan = new CmInteCoLonRepayPlanCi();
                plan.setTenantId(planInfo.getTenantId());
                plan.setEtlDt(org.getToday());
                plan.setLpOrgNo("XIB");
                plan.setDubilNo(planInfo.getLoanId());
                plan.setLonTerm(planInfo.getBillingTenor().longValue());
                plan.setCurrRepayTerm(planInfo.getPayedTenor());
                plan.setRepayStatusCd(repayStatusCd(planInfo.getPlanStatus()));
                plan.setRepayStartDt(planInfo.getIntrStartDate());
                plan.setRepayStopDt(planInfo.getPaymentDueDate());
                plan.setCurrCd("CNY");
                plan.setShouldPayAmt(planInfo.getTotalAmount());
                plan.setPrinTotal(planInfo.getPrincipalAmount());
                plan.setIntTotal(planInfo.getInterestAmount());
                plan.setSrcPlatNo("CCS");
                return plan;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("RepayPlanCiProcessor 出错了：{}", e.getMessage());
        }
        return null;
    }

    /**
     * 转换还款状态吗
     *
     * @param planStatus 分期状态(0-未抛 1-正常 2-逾期 7-提前结清 8-正常结清 9-取消)
     * @return repayStatusCd 06.逾期 25.正常 28.已结清
     */
    private String repayStatusCd(String planStatus) {
        String repayStatusCd = "25";
        if (TransBizConstant.LOAN_PLAN_STATUS_2.equals(planStatus)) {
            repayStatusCd = "06";
        } else if (Arrays.asList(TransBizConstant.LOAN_PLAN_STATUS_6,TransBizConstant.LOAN_PLAN_STATUS_7,TransBizConstant.LOAN_PLAN_STATUS_8).contains(planStatus)) {
            repayStatusCd = "28";
        }
        return repayStatusCd;
    }
}
